#version 130

uniform float x;
uniform float y;
uniform int angle;
uniform sampler2D tex;

in vec2 fragmentTexCoord;
out vec4 fragColor;

uniform float radius = 200.0;

void main(void)
{
	vec2 size = textureSize(tex, 0);
	vec2 center = vec2(x, y);
	vec2 toCenter = fragmentTexCoord * size - center;
	float distance = length(toCenter);

	if (angle < 0) {
		fragColor = texture(tex, fragmentTexCoord);
		return;
	}

	if (distance < radius) {
		float percent = (radius - distance) / radius;
		float gamma = percent * percent * (angle / 20.0) * 8.0;

		float s = sin(gamma);
		float c = cos(gamma);
		toCenter = vec2(dot(toCenter, vec2(c, -s)), dot(toCenter, vec2(s, c)));
	}

	toCenter += center;
	fragColor = texture(tex, toCenter / size);
	return;
}

